package com.bjzsly.star3.xyh.plugin;


import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.bjzsly.star3.shareclick.utils.WeChatUtils;
import com.bjzsly.star3.xyh.auth.TxShareUtils;
import com.bjzsly.star3.xyh.utils.PermissionUtil;
import com.bjzsly.star3.xyh.utils.ToastUtils;
import org.json.JSONArray;

import java.util.UUID;

import io.dcloud.common.DHInterface.IWebview;
import io.dcloud.common.DHInterface.StandardFeature;
import io.dcloud.common.util.JSUtil;
import rx.Observable;
import rx.Observer;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
/**
 * 5+ SDK 扩展插件示例
 * 5+ 扩扎插件在使用时需要以下两个地方进行配置
 * 		1  WebApp的mainfest.json文件的permissions节点下添加JS标识
 * 		2  assets/data/properties.xml文件添加JS标识和原生类的对应关系
 * 本插件对应的JS文件在 assets/apps/H5Plugin/js/test.js
 * 本插件对应的使用的HTML assest/apps/H5plugin/index.html
 *
 * 更详细说明请参考文档http://ask.dcloud.net.cn/article/66
 * **/
public class PGPlugiUtil extends StandardFeature {
    public static  Context actContext;
    private  Context mContext;

    public void onStart(Context pContext, Bundle pSavedInstanceState, String[] pRuntimeArgs) {
        mContext=pContext;
        /**
         * 如果需要在应用启动时进行初始化，可以继承这个方法，并在properties.xml文件的service节点添加扩展插件的注册即可触发onStart方法
         * */
    }

    public void shareQQ(IWebview pWebview, JSONArray array) {  //不带参数 无法成功回调
        //,String title, String summary,  String imgUrl,String targetUrl

        if (TxShareUtils.getInstance().isQQInstall(mContext)){
            String CallBackID = array.optString(0);
            try{
                String title = array.optString(1);
                String summary = array.optString(2);
                String imgUrl = array.optString(3);
                String targetUrl = array.optString(4);
                TxShareUtils.getInstance().shareQQ((Activity) actContext,title,summary,imgUrl,targetUrl);
            }catch (Exception e){
            }
        }else {
            ToastUtils.showToast("请先安装QQ");
        }
    }

    public void shareWX(final IWebview pWebview, JSONArray array) {  //不带参数 无法成功回调
        //,String title, String summary,  String imgUrl,String targetUrl

        if (TxShareUtils.getInstance().isWxInstall()) {
            final String CallBackID = array.optString(0);
            final String title = array.optString(1);
            final String summary = array.optString(2);
            final String imgUrl = array.optString(3);
            final String targetUrl = array.optString(4);
//            TxShareUtils.getInstance().shareQQ((Activity) mContext,title,summary,imgUrl,targetUrl);
            Observable.create(new Observable.OnSubscribe<Object>() {
                @Override
                public void call(Subscriber<? super Object> subscriber) {
                    try {
                        WeChatUtils.configType(actContext, WeChatUtils.TYPE_SHARE_WX_FRIEDN);
                        TxShareUtils.getInstance().shareFriHtml(actContext, title, summary, imgUrl, targetUrl);
                        subscriber.onNext(null);
                    } catch (Exception e) {
                        subscriber.onError(e);
                    }
                }
            }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Object>() {
                @Override
                public void onNext(Object s) {
//                    JSUtil.execCallback(pWebview, CallBackID, "分享成功", JSUtil.OK, false);
//                    ToastUtils.showToast("分享成功");
                }

                @Override
                public void onCompleted() {
                }

                @Override
                public void onError(Throwable error) {
//                    ToastUtils.showToast("分享失败");
//                    JSUtil.execCallback(pWebview, CallBackID, "分享失败", JSUtil.ERROR, false);
                }
            });
        }else {
            ToastUtils.showToast("请先安装微信");
        }
    }

    public void shareCircle(final IWebview pWebview, JSONArray array) {  //不带参数 无法成功回调
        if (TxShareUtils.getInstance().isWxInstall()) {
        final String CallBackID = array.optString(0);
        final String title = array.optString(1);
        final String summary = array.optString(2);
        final String imgUrl = array.optString(3);
        final String targetUrl = array.optString(4);
//            TxShareUtils.getInstance().shareQQ((Activity) mContext,title,summary,imgUrl,targetUrl);
        Observable.create(new Observable.OnSubscribe<Object>() {
            @Override
            public void call(Subscriber<? super Object> subscriber) {
                try {
                WeChatUtils.configType(actContext,WeChatUtils.TYPE_SHARE_WX_CIRCLE);
                TxShareUtils.getInstance().shareCirHtml(actContext,title,summary,imgUrl,targetUrl);
                    subscriber.onNext(null);
                }catch (Exception e){
                    subscriber.onError(e);
                }

            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Object>() {
            @Override
            public void onNext(Object s) {
            }
            @Override
            public void onCompleted() {
            }
            @Override
            public void onError(Throwable error) {

            }
        });
    }else {
            ToastUtils.showToast("请先安装微信");
        }
    }


    public  void randomDeviceUUID(IWebview pWebview, JSONArray array) {
         String CallBackID = array.optString(0);
        UUID uuid;
        final String androidId = getAndroidId(pWebview.getActivity());
        if (androidId != null && !"9774d56d682e549c".equals(androidId)) {
            uuid = UUID.nameUUIDFromBytes(androidId.getBytes());
        } else {
            final String deviceId = getDeviceId(pWebview.getActivity());
            uuid = deviceId != null ? UUID.nameUUIDFromBytes(deviceId.getBytes()) : UUID.randomUUID();
        }
        Log.d("Xiang",uuid.toString());
        JSUtil.execCallback(pWebview, CallBackID, uuid.toString(), JSUtil.OK, false);
    }


    public static String getAndroidId(Context context) {
        try {
            return Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
        } catch (Exception e) {
            Log.e("Xiang",e.getMessage());
        }
        return null;
    }

    public static String getDeviceId(Context context) {
        if (PermissionUtil.checkPermission(context, Manifest.permission.READ_PHONE_STATE)) {
            try {
                TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
                return checkDeviceId(tm.getDeviceId());
            } catch (Exception e) {
               Log.e("Xiang",e.getMessage());
            }
        }
        return null;
    }

    private static String checkDeviceId(String deviceId) {
        if (!TextUtils.isEmpty(deviceId))
            return deviceId;
        try {
            return "35" +
                    Build.BOARD.length() % 10 +
                    Build.BRAND.length() % 10 +
                    Build.CPU_ABI.length() % 10 +
                    Build.DEVICE.length() % 10 +
                    Build.DISPLAY.length() % 10 +
                    Build.HOST.length() % 10 +
                    Build.ID.length() % 10 +
                    Build.MANUFACTURER.length() % 10 +
                    Build.MODEL.length() % 10 +
                    Build.PRODUCT.length() % 10 +
                    Build.TAGS.length() % 10 +
                    Build.TYPE.length() % 10 +
                    Build.USER.length() % 10;
        } catch (Exception e) {
           Log.e("Xiang",e.getMessage());
        }
        return null;
    }


}
